package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysCompanies;
import com.ruoyi.system.service.ISysCompaniesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 地市级公司信息Controller
 * 
 * @author ruoyi
 * @date 2022-07-18
 */
@Api("公司信息管理")
@Controller
@RequestMapping("/system/companies")
public class SysCompaniesController extends BaseController
{
    private String prefix = "system/companies";

    @Autowired
    private ISysCompaniesService sysCompaniesService;

    @RequiresPermissions("system:companies:view")
    @GetMapping()
    public String companies()
    {
        return prefix + "/companies";
    }

    /**
     * 查询地市级公司信息列表
     */
    @ApiOperation("查询地市级公司信息列表")
    @RequiresPermissions("system:companies:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysCompanies sysCompanies)
    {
        startPage();
        List<SysCompanies> list = sysCompaniesService.selectSysCompaniesList(sysCompanies);
        return getDataTable(list);
    }

    /**
     * 导出地市级公司信息列表
     */
    @ApiOperation("导出地市级公司信息列表")
    @RequiresPermissions("system:companies:export")
    @Log(title = "地市级公司信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysCompanies sysCompanies)
    {
        List<SysCompanies> list = sysCompaniesService.selectSysCompaniesList(sysCompanies);
        ExcelUtil<SysCompanies> util = new ExcelUtil<SysCompanies>(SysCompanies.class);
        return util.exportExcel(list, "地市级公司信息数据");
    }

    /**
     * 新增地市级公司信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存地市级公司信息
     */
    @ApiOperation("新增保存地市级公司信息")
    @RequiresPermissions("system:companies:add")
    @Log(title = "地市级公司信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysCompanies sysCompanies)
    {
        return toAjax(sysCompaniesService.insertSysCompanies(sysCompanies));
    }

    /**
     * 修改地市级公司信息
     */
    @RequiresPermissions("system:companies:edit")
    @GetMapping("/edit/{companiesId}")
    public String edit(@PathVariable("companiesId") Long companiesId, ModelMap mmap)
    {
        SysCompanies sysCompanies = sysCompaniesService.selectSysCompaniesByCompaniesId(companiesId);
        mmap.put("sysCompanies", sysCompanies);
        return prefix + "/edit";
    }

    /**
     * 修改保存地市级公司信息
     */
    @ApiOperation("修改保存地市级公司信息")
    @RequiresPermissions("system:companies:edit")
    @Log(title = "地市级公司信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysCompanies sysCompanies)
    {
        return toAjax(sysCompaniesService.updateSysCompanies(sysCompanies));
    }

    /**
     * 删除地市级公司信息
     */
    @ApiOperation("删除地市级公司信息")
    @RequiresPermissions("system:companies:remove")
    @Log(title = "地市级公司信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysCompaniesService.deleteSysCompaniesByCompaniesIds(ids));
    }

    /**
     * 导入地市级公司信息
     */
    @ApiOperation("导入地市级公司信息")
    @RequiresPermissions("system:companies:import")
    @Log(title = "地市级公司信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(@RequestPart MultipartFile file,@RequestParam(defaultValue = "false") boolean updateSupport) throws Exception
    {
        ExcelUtil<SysCompanies> util = new ExcelUtil<SysCompanies>(SysCompanies.class);
        List<SysCompanies> list = util.importExcel(file.getInputStream());
        String message = sysCompaniesService.importCompanies(list, updateSupport, getLoginName());
        return AjaxResult.success(message);
    }

    /**
     * 导出地市级公司信息模板
     */
    @ApiOperation("导出地市级公司信息模板")
    @RequiresPermissions("system:companies:view")
    @Log(title = "地市级公司信息", businessType = BusinessType.EXPORT)
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<SysCompanies> util = new ExcelUtil<SysCompanies>(SysCompanies.class);
        return util.importTemplateExcel("地市级公司信息");
    }
}
